package com.lrm.dao;

import com.lrm.NotFoundException;
import com.lrm.po.Blog;
import com.lrm.po.Type;
import com.lrm.util.DBUtils;
import com.lrm.dao.BlogController;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TypeController {
        public static Type findOne(Long id){
            Type type = null;
            try {
                Connection con = DBUtils.getConnection();
                String sql = "select * from t_type where id = ?";
                PreparedStatement ps = con.prepareStatement(sql);
                ps.setObject(1, id);
                ResultSet result = ps.executeQuery();
                int i = 0;
                while (result.next()) {
                    if (i < 1) {
                        type = new Type();
                        type.setId(result.getLong("id"));
                        type.setName(result.getString("name"));
                        List<Blog> blogs = new ArrayList<>();
                        sql = "select * from t_blog where type_id = ?";
                        PreparedStatement ps1 = con.prepareStatement(sql);
                        ps1.setObject(1, id);
                        ResultSet result1 = ps1.executeQuery();
                        while (result1.next()) {
                            Blog blog = new Blog();
                            blog.setId(result1.getLong("id"));
                            blog.setAppreciation(result1.getBoolean("appreciation"));
                            blog.setCommentabled(result1.getBoolean("commentabled"));
                            blog.setContent(result1.getString("content"));
                            blog.setCreateTime(result1.getDate("create_time"));
                            blog.setDescription(result1.getString("description"));
                            blog.setFirstPicture(result1.getString("first_picture"));
                            blog.setFlag(result1.getString("flag"));
                            blog.setPublished(result1.getBoolean("published"));
                            blog.setRecommend(result1.getBoolean("recommend"));
                            blog.setShareStatement(result1.getBoolean("share_statement"));
                            blog.setTitle(result1.getString("title"));
                            blog.setUpdateTime(result1.getDate("update_time"));
                            blog.setViews(result1.getInt("views"));
                            blog.setType(type);
                            blog.setUser(UserController.findOne(result1.getLong("user_id")));
                            blogs.add(blog);
                        }
                        type.setBlogs(blogs);
                    } else {
                        break;
                    }
                }
            }
            catch (Exception e){
                throw new RuntimeException(e);
            }
            return type;
        }

    public static Type save(Type t){
        try{
            Connection con= DBUtils.getConnection();
            String sql="call insert_type("+t.getId()+","+"'"+t.getName()+"'"+");";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.execute();
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return t;
    }

    public static Type findByName(String name){
        Type type=null;
        try{
            Connection con= DBUtils.getConnection();
            String sql="select * from t_type where name=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setObject(1,name);
            ResultSet result=ps.executeQuery();

            if(result.next()){
                type=new Type();
                type.setName(result.getString("name"));
                type.setId(result.getLong("id"));
            }
        }catch(Exception e){
            throw new RuntimeException(e);
        }
        return type;
    }

    public static void delete(Long id){
        try{
            Connection con= DBUtils.getConnection();
            String sql="call delete_type("+id+");";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.execute(sql);
        }catch(Exception e){
            throw new RuntimeException(e);
        }
    }

    public static Type updateType(Long id,String name){
        Type t = TypeController.findOne(id);
        if (t == null) {
            throw new NotFoundException("不存在该标签");
        }
        try{
            Connection con= DBUtils.getConnection();
            String sql="update t_type set name=? where id=?";
            PreparedStatement ps=con.prepareStatement(sql);
            ps.setObject(1,name);
            ps.setObject(2,id);
            ps.executeUpdate();
            t.setName(name);
        }catch(Exception e){
            e.printStackTrace();
        }
        return t;
    }
}
